<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :beforeClose="handleClose"
    :visible.sync="dialogVisible"
    width="450px">
    <el-form :model="goodsForm" size="mini" label-width="130px" :rules="rules" ref="goodsForm">
      <el-form-item label="产品线：" prop="goodsLineId">
        <el-select v-model="goodsForm.goodsLineId" filterable clearable>
          <el-option v-for="(item,index) in goodsLineList" :key="index" :label="item.cddValue" :value="item.cddId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="牌号：" prop="goodsKingId">
        <el-select v-model="goodsForm.goodsKingId" filterable placeholder="请选择" clearable>
          <el-option
            v-for="item in goodsList"
            :key="item.kingId"
            :label="item.goodsSn"
            :value="item.kingId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产能(吨/每月)：">
        <!-- <el-input v-model="goodsForm.productionQuantity" clearable style="width:200px"></el-input> -->
        <el-input-number v-model="goodsForm.productionQuantity" :controls="false" :min="0" style="width:200px"></el-input-number>
      </el-form-item>
      <el-form-item label="库存(吨/月)：">
        <!-- <el-input v-model="goodsForm.supplierStock" clearable style="width:200px"></el-input> -->
        <el-input-number v-model="goodsForm.supplierStock" :controls="false" :min="0" style="width:200px"></el-input-number>
      </el-form-item>
      <el-form-item label="产业链：">
        <el-input v-model="goodsForm.industrialChain" clearable style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label="对标产品：">
        <el-input v-model="goodsForm.benchmarkingProduct" clearable style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label="对标工厂：">
        <el-input v-model="goodsForm.benchmarkingFactory" clearable style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label="销售模式：">
          <el-select v-model="goodsForm.saleModel">
            <el-option value="竞拍" label="竞拍"></el-option>
            <el-option value="大单" label="大单"></el-option>
            <el-option value="预售" label="预售"></el-option>
            <el-option value="底仓" label="底仓"></el-option>
            <el-option value="产销平衡" label="产销平衡"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="投产日期：" prop="dateType">
        <el-radio-group v-model="goodsForm.dateType" >
          <el-radio :label="0">无投产日期</el-radio>
          <el-radio :label="1">日期</el-radio>
        </el-radio-group>
        <el-form-item label-width="0px" v-if="goodsForm.dateType == 1" prop="productionDate" >
          <el-date-picker
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            v-model="goodsForm.productionDate"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form-item>

      <el-form-item label="负责人：">
        <el-select v-model="goodsForm.purchaseUserId" filterable clearable>
          <el-option v-for="(item,index) in userOptionList" :key="index" :label="item.userName" :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  getCustomerSetting,
  getOrderUsersList,
  orderGoodsList
} from '@/api/partner';
import {
  supplierGoodsAdd,
} from '@/api/company';
import { validatePhone } from '@/utils/validate';
import {mapGetters} from "vuex";
const defaultForm = {
  supplierId:'',
  goodsKingId: '',//牌号ID
  goodsLineId:'',//产品线ID
  productionQuantity:'',//产能
  supplierStock: '',//库存(吨/月)
  industrialChain:'',//产业链
  purchaseUserId: '',//采购负责人
  benchmarkingProduct:'',// 对标产品
  benchmarkingFactory:'',//  对标工厂
  saleModel:'',//   销售模式
  productionDate:'',//投产日期
  dateType:'',//投产日期类型
}
export default {
  name: 'addSupplierGoods',
  data() {
    const isvalidatePhone = (rule, value, callback) => {
      if (!validatePhone(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    };
    return {
      title: '',
      dialogVisible: false,
      btnLoading:false,
      goodsForm: JSON.parse(JSON.stringify(defaultForm)),
      goodsLineList: [],
      userOptionList:[],
      rules: {
        goodsLineId: [{ required: true, message: '请选择产品线', trigger: 'change' }],
        goodsKingId: [{ required: true, message: '请选择牌号', trigger: 'change' }],
        productionQuantity: [{ required: true, message: '请输入产能', trigger: 'blur' }],
        dateType:[{ required: true, message: '请选择投产日期', trigger: 'change' }],
        productionDate:[{ required: true, message: '请选择投产日期', trigger: 'change' }],
      },
      goodsList:[]
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
  },
  methods: {
    getGoodsList(){
      orderGoodsList().then((res)=>{
        this.goodsList = res.list
      })
    },
    //获取采购负责人下拉选项
    getUserOptionList(){
      getOrderUsersList({
        departmentType:"purchase"
      }).then((res) => {
        this.userOptionList = res;
      })
    },
    // 获取供应商配置
    getCustomerSettingList(){
      getCustomerSetting().then((res) =>{
        res.forEach((el) => {
          if (el.cdtCode == 'goods_line') {//产品线
            this.goodsLineList = el.data
          }
        });
      })
    },
    showPop(type,item) {
      this.dialogVisible = true;
      this.getCustomerSettingList();
      this.getUserOptionList();
      this.getGoodsList()
      if (type == 'add') {
        this.title = '新增生产信息';
        this.goodsForm = JSON.parse(JSON.stringify(defaultForm));
        this.goodsForm.supplierId = item;
        this.goodsForm.purchaseUserId = this.userId
      } else if (type == 'edit') {
        this.title = '编辑生产信息';
        for (let key in defaultForm) {
          this.goodsForm[key] = item[key] || item[key] == 0 ? item[key] : defaultForm[key];
        }
        if(this.goodsForm.productionDate=== ''){
          this.goodsForm.dateType = ''
        }else{
          this.goodsForm.dateType = this.goodsForm.productionDate != 0 ? 1 : 0
        }
      }
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.goodsForm.resetFields();
    },
    handleConfirm() {
      this.$refs.goodsForm.validate((valid) => {
        const data = JSON.parse(JSON.stringify(this.goodsForm))
        data.productionDate = data.dateType == 0 ? 0 : data.productionDate
        if (valid) {
          this.btnLoading = true;
          supplierGoodsAdd(data).then((res) => {
            this.$message.success('操作成功');
            this.handleClose();
            this.$emit('update');
            this.btnLoading = false;
          }).catch(() => {
            this.btnLoading = false;
          })
        }
      })
    },
  },
}
</script>
<style scoped>

</style>
